package cn.edu.lingnan.tourism.controller;

import cn.edu.lingnan.tourism.exeption.TourismException;
import cn.edu.lingnan.tourism.pojo.Favorite;
import cn.edu.lingnan.tourism.pojo.Route;
import cn.edu.lingnan.tourism.pojo.User;
import cn.edu.lingnan.tourism.service.FavoriteService;
import cn.edu.lingnan.tourism.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/favorite")
public class FavoriteController {
    @Resource
    private FavoriteService favoriteService;
    @RequestMapping("/myFavoriteList/{page}")
    public String myFavoriteList(@PathVariable("page")Integer page, HttpSession session) throws TourismException {
        User user = (User) session.getAttribute("user");
        PageHelper.startPage(page,8);
        List<Route> userFavoritesRoute = favoriteService.getUserFavoritesRoute(user.getUid());
        return new Result<List<Route>>().success(new PageInfo<>(userFavoritesRoute));
    }
    @RequestMapping("/deleteFavorite")
    public String deleteFavorite(@RequestBody Favorite favorite){
        try{
            favoriteService.removeFavorite(favorite);
            return new Result<>().success("删除成功");
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }

    }
    @RequestMapping("/addFavorite")
    public String addFavorite(@RequestBody Favorite favorite){
        try{
            favoriteService.addFavorite(favorite);
            return new Result<>().success("添加成功");
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/checkFavorite/{rid}")
    public String checkFavorite(@PathVariable("rid")Integer rid,HttpSession session){
        User user = (User) session.getAttribute("user");
        Favorite favorite = new Favorite(rid,null, user.getUid());
        Favorite favoriteRouteByUidAndRid = favoriteService.getFavoriteRouteByUidAndRid(favorite);
        if(favoriteRouteByUidAndRid==null){
            return new Result<>().error("未收藏");
        }
        return new Result<>().success("已收藏");
    }
    @RequestMapping("/getHotRoute")
    public String getHotRoute(){
        List<Route> favoriteRouteHost = favoriteService.getFavoriteRouteHost();
        return new Result<>().success(favoriteRouteHost);
    }
}
